package com.longdaji.decoration.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/3/5 0005.
 */

public class PhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_CONTENT = 1;
    private static final int VIEW_TYPE_FOOTER = 2;

    private List<Uri> mUris;
    private Context mContext;

    public PhotoAdapter(List<Uri> uris) {
        mUris = getProcessed(uris);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        if (viewType == VIEW_TYPE_FOOTER) {
            PhotoFooterHolder holder = new PhotoFooterHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_footer, parent, false));
            return holder;
        } else {
            return new PhotoContentHolder(LayoutInflater.from(mContext).inflate(R.layout.item_photo, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PhotoContentHolder) {
            Glide.with(mContext).load(mUris.get(position)).into(((PhotoContentHolder) holder).ivPhoto);
        }
    }

    @Override
    public int getItemCount() {
        return mUris == null ? 0 : mUris.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mUris.size() - 1) {
            return VIEW_TYPE_FOOTER;
        } else {
            return VIEW_TYPE_CONTENT;
        }
    }


    class PhotoContentHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_photo)
        ImageView ivPhoto;

        public PhotoContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class PhotoFooterHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.view_footer)
        RelativeLayout viewFooter;

        public PhotoFooterHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

        @OnClick(R.id.view_footer)
        void onViewClicked() {
            Log.d("mytest", "11111111111");
            addPhotoListener.addPhoto();
        }

    }

    private List<Uri> getProcessed(List<Uri> list) {
        List<Uri> result = new ArrayList<>();
        result.addAll(list);
        Uri uri = Uri.parse("");
        result.add(uri);
        Log.e("mytest", "" + result.size());
        return result;
    }

    public void setData(List<Uri> uris) {
        mUris.clear();
        mUris.addAll(getProcessed(uris));
        notifyDataSetChanged();
    }

    public boolean isFooter(int position) {
        return getItemViewType(position) == VIEW_TYPE_FOOTER;
    }

    public boolean isContent(int position) {
        return getItemViewType(position) == VIEW_TYPE_CONTENT;
    }

    private AddPhotoListener addPhotoListener = null;

    public interface AddPhotoListener {
        void addPhoto();
    }

    public void setAddPhotoListener(AddPhotoListener listener) {
        addPhotoListener = listener;
    }


}
